package getform.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import getform.entity.Users;

public class RegServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		Users u = new Users();
		String username,mypassword,gender,email,introduce,isAccept;
		Date birthday;
		String[] favorites;
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			username=request.getParameter("username");
			mypassword=request.getParameter("mypassword");
			gender=request.getParameter("gender");
			email=request.getParameter("email");
			introduce=request.getParameter("introduce");
			birthday = sdf.parse(request.getParameter("birthday"));
			//用来获取多个复选按钮的值
			favorites = request.getParameterValues("favorite");
			if (request.getParameterValues("flag")!=null) {
				isAccept  = request.getParameter("flag");
			}
			else{
				isAccept="false";
			}
			
			u.setUsername(username);
			u.setMypassword(mypassword);
			u.setGender(gender);
			u.setEmail(email);
			u.setFavorites(favorites);
			u.setIntroduce(introduce);
			u.setBirthday(birthday);
			u.setFlag(Boolean.parseBoolean(isAccept));
			//把注册成功的用户对象保存在session中
			request.getSession().setAttribute("regUser", u);
			//跳转到注册成功页面
			request.getRequestDispatcher("../../userinfo.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
	}

	@Override
	public void destroy() {
		
		super.destroy();
	}

	@Override
	public void init() throws ServletException {
		
		super.init();
	}
	
}
